<?php
/**
 * The Crux of our User Code section
 *
 *
 **/

// XXX this should really be in a util class
function clean( $string ) {
  return ($string);
}

function isEmail( $string ) {

}

function smurfPrep( $string, $validator ) {
  $count = 1;
  // XXX url not supported. Need another regex
  while( $count ) {
    // I don't like the not lookahead. Any way to capture lookaheads and replace
    $string = preg_replace('/\[([a-zA-Z]*)\](.*)\[\/\1\](?!.*\[\/\1\])/', " [$1:${validator}]$2[/\$1:${validator}] ", $string, -1, $count );
  }
  return $string;
}
function unsmurf( $string, $validator ) {

  $string = str_replace( ":${validator}", '', $string ); 
  return $string;
}

function smurf( $string, $valid ) {

  $string = nl2br(stripslashes($string));

  $codes = array ( "[url:$valid \"", "\"]:valid", "[/url:$valid]",
                   "[style:$valid \"", "[/style:$valid]",
                   "[b:$valid]", "[/b:$valid]",
                   "[i:$valid]", "[/i:$valid]",
                   "[u:$valid]", "[/u:$valid]",
                   "[img:$valid]", "[/img:$valid]",
                   "[code:$valid]", "[/code:$valid]",
                   "[quote:$valid]", "[/quote:$valid]",
                   ":p$valid"
                  );
  $htmls = array ( '<a href="', '">', '</a>',
                   '<span class="post.', '</span>',
                   '<strong class="post">', '</strong>',
                   '<em class="post">', '</em>',
                   '<u class="post">', '</u>',
                   '<img class="post" src="', '" />',
                   '<pre class="code post">', '</pre>',
                   '<p class="post quote" >', '</p>',
                   '<img src="smile.png" />'
                 );
  $string= str_replace( $codes, $htmls, $string ); 

  return $string;
}

function genUUID() {
  // stolen from someone on sitepoint who stole it from someone else :p
  return sprintf( "%04x%04x%04x%04x%04x%04x%04x%04x",
         mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
         mt_rand( 0, 0x0fff ) | 0x4000,
         mt_rand( 0, 0x3fff ) | 0x8000,
         mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
}

?>
